package com.springboot.controller;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.*;
import com.springboot.dto.VerifyDTO;
import com.springboot.dto.VerifyDemandDTO;
import com.springboot.repository.*;
import com.springboot.service.EnterpriseService;
import com.springboot.service.PersonalService;
import com.springboot.service.PlatformService;
import com.springboot.tools.ResultUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bank")
public class PlatformController {

    private PlatformService platformService;
    private final PersonRepository personRepository;
    private final EnterpriseRepository enterpriseRepository;
    private final BankRepository bankRepository;
    private DemandSubRepository demandSubRepository;
    private EnterpriseService enterpriseService;
    private PersonalService personalService;
    private DemandRepository demandRepository;

    @Autowired
    public PlatformController(PlatformService platformService,PersonRepository personRepository,EnterpriseRepository enterpriseRepository,BankRepository bankRepository,DemandSubRepository demandSubRepository,EnterpriseService enterpriseService, DemandRepository demandRepository,PersonalService personalService) {

        this.platformService = platformService;
        this.personRepository=personRepository;
        this.enterpriseRepository=enterpriseRepository;
        this.bankRepository=bankRepository;
        this.demandSubRepository=demandSubRepository;
        this.enterpriseService = enterpriseService;
        this.demandRepository = demandRepository;
        this.personalService=personalService;
    }

    //个人完善信息待审核列表
    @ResponseBody
    @GetMapping(value = "/platform/mopersonalServicedifyPersonList")
    public Result modifyPersonList(@ApiParam Pageable pageable) {
        Page<BtPerson> person = personRepository.findByStatus(CommonConstant.UNVERIFY,pageable);
        return ResultUtil.success(person);
    }



    //企业完善信息待审核列表
    @ResponseBody
    @GetMapping(value = "/platform/modifyEnterpriseList")
    public Result modifyEnterpriseList(@ApiParam Pageable pageable) {
        Page<BtEnterprise> enterprises = enterpriseRepository.findByStatus(CommonConstant.UNVERIFY,pageable);
        return ResultUtil.success(enterprises);
    }


    //银行完善信息待审核列表
    @ResponseBody
    @GetMapping(value = "/platform/modifyBankList")
    public Result modifyBankList(@ApiParam Pageable pageable) {
        Page<BtBank> bank = bankRepository.findByStatus(CommonConstant.UNVERIFY,pageable);
        return ResultUtil.success(bank);
    }


    @ResponseBody
    @GetMapping(value = "/person/logout/list")
    public Result logoutList(@ApiParam Pageable pageable){
        Page<BtPerson> pages = personRepository.findByStatus(CommonConstant.LOGOUTING,pageable);
        return ResultUtil.success(pages);
    }
    //完善信息审核
    @ResponseBody
    @PostMapping(value = "/platform/verifyInformation")
    public Result modifyEnterprise(@Valid @RequestBody VerifyDTO verifyDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        platformService.verifyInformation(verifyDTO);
        return ResultUtil.success("审核完成");
    }

    //银行demandSub列表
    @ResponseBody
    @GetMapping(value = "/platform/demandSubList")
    public Result demandSubList(@ApiParam Pageable pageable) {
        Page<BtDemandSub> btDemandSub = demandSubRepository.findByPlatShown(CommonConstant.DEMAND_SHOWN,pageable);
        return ResultUtil.success(btDemandSub);
    }

    //注销审核
    @ResponseBody
    @PostMapping(value = "/logout/verify")
    public Result verifyLogout(@Valid @RequestBody VerifyDTO verifyDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        platformService.verifyLogout(verifyDTO);
        return ResultUtil.success();
    }

    /**
     * demand list
     * @param pageable
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/demand/list")
     public Result demandsForNewAndMod(@ApiParam Pageable pageable){
        Page<BtDemand> pages = platformService.demandsForNewAndMod(pageable);
        return ResultUtil.success(pages);
     }

    @ResponseBody
    @PostMapping(value = "/demand/verify")
    public Result verifyDemand(@RequestBody VerifyDemandDTO verifyDemandDTO){
         platformService.verifyDemand(verifyDemandDTO);
        return ResultUtil.success("审核成功");
    }

    @ResponseBody
    @GetMapping(value = "/unsigned/list")
    public Result unsignedlist(@ApiParam Pageable pageable){
        Page<BtDemand> demands = demandRepository.findByStatus(pageable, CommonConstant.UNSIGNED);
        return ResultUtil.success(demands);
    }

    //首页模糊查询(需求/银行/个人/企业)
    @ResponseBody
    @GetMapping(value = "/platform/fuzzyQuery")
    public Result findEnterprise(@RequestParam(value = "type", defaultValue = "") String type,
                                 @RequestParam(value = "name", defaultValue = "") String name) {

        if (type != null & type.equals("demand")) {
            List<BtDemand> demands = demandRepository.findDemand(name);
            return ResultUtil.success(demands);
        } else if (type != null & type.equals("bank")) {
            List<BtBank> banks = bankRepository.findBank(name);
            return ResultUtil.success(banks);
        } else if (type != null & type.equals("person")) {
            List<BtPerson> person = personRepository.findPerson(name);
            return ResultUtil.success(person);
        } else if (type != null & type.equals("enterprise")) {
            List<BtEnterprise> enterprises = enterpriseRepository.findEnterprise(name);
            return ResultUtil.success(enterprises);
        }
        return ResultUtil.error("没有查找到符合类型的数据");
    }

}
